/**
 * 项目管理路由
 *
 * Created by snowingsea on 2020/02/06.
 */
const middleware = require('./../middleware');
const projects = require('./../../models/projects');
const PhoenixError = require('./../../models/phoenix-error');

/**
 * 获取项目信息
 * @param req
 * @param res
 * @param next
 */
function getProjectsNamesSet(req, res, next) {
  const { userId } = req.query;
  projects.namesSet(userId, { lastProjectOnly: !userId })
    .then((names) => {
      res.json({ names });
    })
    .catch((err) => {
      next(new PhoenixError(err));
    });
}

/**
 * Export model definition object.
 */
module.exports = (app, prefix) => {
  const projectsNamesSetURL = `${prefix}/projects/names-set.json`;

  // 获取项目名称集合（去重）
  app.get(projectsNamesSetURL, middleware.user.requireTokenUnexpired);
  app.get(projectsNamesSetURL, middleware.user.requireAvailable);
  app.get(projectsNamesSetURL, middleware.user.requireTestManager);
  app.get(projectsNamesSetURL, getProjectsNamesSet);
};
